package com.Fragment;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import androidx.fragment.app.Fragment;

import com.Dao.NewsDao;
import com.EventHandler.BtnEvent;
import com.example.myapplication.R;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link MineFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class MineFragment extends Fragment {

    // TODO: Rename parameter arguments, choose names that match
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER

    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";
    // TODO: Rename and change types of parameters
    private String mParam1;  //mParam1 和 mParam2 用于接收从外部传入的参数
    private String mParam2;
    private NewsDao newsDao;  //newsDao 和 btnEvent 用于处理业务逻辑
    private BtnEvent btnEvent;
    private TextView usernametextView;  //用于显示用户名
    private SharedPreferences sharedPreferences;  //用于存储和获取用户信息
    private String username;  //用于保存当前用户名
    public MineFragment() {
        // Required empty public constructor
    }

    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @param param1 Parameter 1.
     * @param param2 Parameter 2.
     * @return A new instance of fragment MineFragment.
     */
    // TODO: Rename and change types and number of parameters
    //用于创建 Fragment 实例
    public static MineFragment newInstance(String param1, String param2) {
        MineFragment fragment = new MineFragment();  //实例化Fragment，创建了一个MineFragment的实例
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }


    @Override
    public void onCreate(Bundle savedInstanceState) {
        //调用父类的 onCreate() 方法
        // 确保 Fragment 的基本初始化工作得以完成
        super.onCreate(savedInstanceState);
        sharedPreferences = requireContext().getSharedPreferences("user_info", Context.MODE_PRIVATE);
        //读取用户名
        username = sharedPreferences.getString("username",null);
        //使用 getArguments() 方法检查当前 Fragment 是否包含传递的参数
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }
    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        // 初始化与视图相关的组件
        usernametextView = view.findViewById(R.id.username_textview);
        usernametextView.setText(username);
        newsDao = new NewsDao(requireContext());
        btnEvent = new BtnEvent(view,requireContext());
        setUpButtonListener(view); // 将视图作为参数传递
    }
    public void setUpButtonListener(View view) {
        Button commentedbtn = view.findViewById(R.id.commented_btn);
        Button likedbtn = view.findViewById(R.id.liked_btn);
        Button addDataButton = view.findViewById(R.id.add_news_btn);
        Button setAppbtn = view.findViewById(R.id.set_btn);
        btnEvent.initializeUI();
        btnEvent.setListeners();
        /*
        //查看评论过的新闻
        commentedbtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = null;
                intent = new Intent(requireContext(), CommentNewsActivity.class);//跳转界面
                startActivity(intent);
            }
        });
        //查看点赞过的评论/新闻
        likedbtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = null;
                intent = new Intent(requireContext(), LikedNewsActivity.class);//跳转界面
                startActivity(intent);
            }
        });

        addDataButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.d("ButtonClick", "addDataButton 被点击了");
                String title = "周末这些重要消息将影响股市";
                String content="沪深交易所在5月重启，北交所在6月重启。有的上市委会议已经通过了IPO的首发申请。";
                long currentTimeMillis = System.currentTimeMillis();
                News news = new News(title,content,"热点","fjh",currentTimeMillis,null);
                //插入数据
                long flag = newsDao.insertNews(news);
                if(flag==-1){
                    Toast.makeText(requireContext(), "添加新闻失败！", Toast.LENGTH_SHORT).show();//显示登录成功的弹窗，简单写法
                }
                else{
                    Toast.makeText(requireContext(), "添加新闻成功！", Toast.LENGTH_SHORT).show();//显示登录成功的弹窗，简单写法
                }
            }
        });*/
    }
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_mine, container, false);
    }
}